﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.News
{
    public partial class News : HFController.BagePages
    {
        /// <summary>
        /// 新闻列表
        /// </summary>
        public List<hf.Model.News> list = new List<hf.Model.News>();

        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

            //是否首次加载
            if (!Page.IsPostBack)
            {
                //绑定发布者
                tbByUser.Text = this.getAdminInfo().UID.ToString();

                //绑定法律事务
                this.bindLegalAffairs();
                
                //设置默认的法律事务大类
                dropbClassID.Items.Insert(0, new ListItem("--请选择--", "0"));

                //绑定专题
                this.bindTopicClass();

                //绑定省份城市
                DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindProvinceID();

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        NewsController.getController().doDelegeNews(ID);
                        break;
                }
            }
        }

        public void bindProvinceID()
        {
            DropProvinceID.DataSource = CityController.getController().getProvinceList(100);
            DropProvinceID.DataValueField = "ID";
            DropProvinceID.DataTextField = "ProvinceName";
            DropProvinceID.DataBind();
            DropProvinceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        public void bindCityID()
        {
            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定法律事务
        /// </summary>
        private void bindLegalAffairs()
        {
            dropClassID.DataSource = LegalAffairsController.getController().getLegalAffairsList(100);//绑定数据源（下拉框支持List<>类型直接绑定）
            dropClassID.DataValueField = "ID"; //下拉列表的值，用他写到数据库
            dropClassID.DataTextField = "LegalAffairsTitle"; //下拉列表的显示文字，和要绑定的数据库字段一致
            dropClassID.DataBind(); //执行绑定
            dropClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 法律事务选择框发生改变的时候触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dropClassID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindLegalAffairsBiglClass(int.Parse(this.dropClassID.SelectedValue));
        }
        
        /// <summary>
        /// 根据法律事务的ID，绑定法律事务大分类
        /// </summary>
        /// <param name="LegalAffairsID"></param>
        private void bindLegalAffairsBiglClass(int LegalAffairsID)
        {
            dropbClassID.DataSource = LegalAffairsController.getController().getLegalAffairsBiglClass(2000, "LegalAffairsID=" + LegalAffairsID.ToString());//绑定数据源（下拉框支持List<>类型直接绑定）
            dropbClassID.DataValueField = "ID"; //下拉列表的值，用他写到数据库
            dropbClassID.DataTextField = "ClassName"; //下拉列表的显示文字，和要绑定的数据库字段一致
            dropbClassID.DataBind(); //执行绑定
            dropbClassID.Items.Insert(0 ,new ListItem("--请选择--" ,"0"));
        }

        /// <summary>
        /// 绑定新闻专题
        /// </summary>
        private void bindTopicClass()
        {
            dropTopicID.DataSource = NewsController.getController().getTopicList(2000);
            dropTopicID.DataTextField = "TopicName";
            dropTopicID.DataValueField = "ID";
            dropTopicID.DataBind();
            dropTopicID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定需要修改的数据
        /// </summary>
        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.News model = NewsController.getController().getNewsModel(ID);

            dropClassID.SelectedValue = model.ClassID.ToString();
            this.bindLegalAffairsBiglClass(model.ClassID);
            dropbClassID.SelectedValue = model.bClassID.ToString();
            dropTopicID.SelectedValue = model.TopicID.ToString();
            tbTitle.Text = model.Title;
            imgPicTitle.ImageUrl = this.getUpFileFolder + model.PicTitle;
            tbOverview.Text = model.Overview;
            tbContent.Text = model.Content;
            tbHits.Text = model.Hits.ToString();
            tbByForm.Text = model.ByFrom;
            tbByUser.Text = model.ByUser;
            rblisIndex.SelectedValue = model.isIndex.ToString();
            rblisRotation.SelectedValue = model.isRotation.ToString();
            rblisTop.SelectedValue = model.isTop.ToString();

            hf.Model.City city = CityController.getController().getCityList().Find(i=>i.ID == model.CityID);

            DropProvinceID.SelectedValue = CityController.getController().getProvinceList().Find(i => i.ID == city.ProvinceID).ID.ToString();

            DropCityID.DataSource = CityController.getController().getCityList(100,DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));

            DropCityID.SelectedValue = model.CityID.ToString();

            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                //修改新闻
                NewsController.getController().doModifyNews(int.Parse(IDS.Value), 
                    dropClassID.SelectedValue, 
                    dropbClassID.SelectedValue, 
                    "0", 
                    dropTopicID.SelectedValue, 
                    tbTitle.Text, FileUpload1, 
                    tbOverview.Text, 
                    tbContent.Text, 
                    tbByUser.Text, 
                    tbByForm.Text, int.Parse(rblisIndex.SelectedValue), 
                    tbHits.Text, 
                    int.Parse(rblisRotation.SelectedValue) ,
                    int.Parse(rblisTop.SelectedValue),
                    int.Parse(DropCityID.SelectedValue));
            }
            else {
                //添加新闻
                NewsController.getController().doAddNews(dropClassID.SelectedValue,
                    dropbClassID.SelectedValue,
                    "0",
                    dropTopicID.SelectedValue,
                    tbTitle.Text,
                    FileUpload1,
                    tbOverview.Text,
                    tbContent.Text,
                    tbByUser.Text,
                    tbByForm.Text,
                    int.Parse(rblisIndex.SelectedValue),
                    int.Parse(rblisRotation.SelectedValue) ,
                    int.Parse(rblisTop.SelectedValue),
                    isGo.SelectedValue ,
                    int.Parse(DropCityID.SelectedValue));
            }
        }

        protected void DropProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindCityID();
        }
        
    }
}